package kr.co.sdsway2.web.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.sdsway2.comm.SdsWay2Util;
import kr.co.sdsway2.common.AbstractContraller;
import kr.co.sdsway2.service.PollAswService;
import kr.co.sdsway2.vo.PollAswInfVO;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PollAswController extends AbstractContraller {

	@Resource(name="pollAswService")
	PollAswService pollAswService;
	
	@RequestMapping("pollAswTestMain")
	public ModelAndView pollAswTestMain(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		ModelAndView mav = new ModelAndView("pollAswTest.jsp");
		return mav;
	}

	@RequestMapping("registPollObjAswList")
	public void registPollObjAswList(Model model, HttpServletRequest req) throws JsonProcessingException, IOException{
		
		String objAswList = req.getParameter("objAswList");
		String sPollId = req.getParameter("pollId");
		String sPtId = req.getParameter("ptId");
		if (SdsWay2Util.isNull(objAswList) 
			|| SdsWay2Util.isNull(sPollId)
			|| SdsWay2Util.isNull(sPtId)) {
			return;
		}
		
		int pollId = Integer.parseInt(sPollId);
		int ptId = Integer.parseInt(sPtId);
		
		List<PollAswInfVO> aswVOList = new ArrayList<PollAswInfVO>();
		ObjectMapper om = new ObjectMapper();
		JsonNode jn = om.readTree(objAswList);
		if (jn.isArray()) {
			Iterator<JsonNode> i = jn.iterator();
			while (i.hasNext()) {
				JsonNode objAsw = i.next();
				Iterator<String> ids = objAsw.getFieldNames();
				if (ids.hasNext()) {
					String id = ids.next();
					PollAswInfVO inVO = new PollAswInfVO();
					
					inVO.setPtId(ptId);
					inVO.setAvatarId(id);
					inVO.setPollId(pollId);
//					inVO.setAswNum(objAsw.findValue(id).getTextValue());
					aswVOList.add(inVO);
				}
			}
//			pollAswService.insertPollObjAswInfList(aswVOList);
		}
	}
	
	@RequestMapping("registPollSubAsw")
	public void registPollSubAswList(Model model, HttpServletRequest req){
		
		String sPollId = req.getParameter("pollId");
		String avatarId = req.getParameter("avatarId");
		String sPtId = req.getParameter("ptId");
		String aswVal = req.getParameter("aswVal");
		if (SdsWay2Util.isNull(sPollId)
			|| SdsWay2Util.isNull(avatarId)
			|| SdsWay2Util.isNull(sPtId)) {
			return;
		}
		
		PollAswInfVO inVO = new PollAswInfVO();
		inVO.setPtId(Integer.parseInt(sPtId));
		inVO.setPollId(Integer.parseInt(sPollId));
		inVO.setAvatarId(avatarId);
		inVO.setAswVal(aswVal);
		
		//pollAswService.insertPollAswInfList(inVO);
	}
}
